package itqf.controller;


import cn.dsna.util.images.ValidateCode;
import com.google.gson.Gson;
import itqf.anno.ContentType;
import itqf.entity.Users;
import itqf.service.UserServiceImpl;
import itqf.service.impl.UserService;
import itqf.utils.ActiveCodeUtils;
import itqf.utils.SysConstant;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

/**
 * ${PACKAGE_NAME}
 * 2020/9/14
 */
@WebServlet(name = "UserServlet",value = "/userController")
public class UserController extends BaseController {
    private UserService userService = new UserServiceImpl();



    public String  activeAccount(HttpServletRequest request, HttpServletResponse response) {
        //激活账户
        String c = request.getParameter("c");
        byte[] dec = Base64.getDecoder().decode(c);
        String sc = new String(dec);
        try {
            int current = userService.activeAccount(sc);
            if(current== 0){
                request.setAttribute("msg","激活成功");
            }else{
                request.setAttribute("msg","激活失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return SysConstant.FORWARD+SysConstant.FORWARD+"message.jsp";
    }

    public String register(HttpServletRequest request, HttpServletResponse response) {
        //注册
        Users user = new Users();
        try {
            BeanUtils.populate(user,request.getParameterMap());
            user.setUstatus(SysConstant.NOT_ACTIVE);
            user.setUcode(ActiveCodeUtils.getActiveCode());
            user.setUrole(SysConstant.CUSTOMER);
            int current = userService.insert(user);
            if(current>0){
                return SysConstant.REDIRECT+SysConstant.FORWARD+"registerSuccess.jsp";
                //response.sendRedirect("registerSuccess.jsp");
            }else{
                request.setAttribute("msg","注册失败");
                return SysConstant.FORWARD+SysConstant.FORWARD+"register.jsp";
                //request.getRequestDispatcher("register.jsp").forward(request,response);
            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public String userlogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //登录
        String username = request.getParameter("username");
        String upassword = request.getParameter("upassword");
        Users user = userService.login(username, upassword);
        try {
            if(user!=null&&user.getUstatus()== SysConstant.NOT_ACTIVE){
                request.setAttribute("msg","账户没激活");
                //request.getRequestDispatcher("/login.jsp").forward(request,response);
            }else if(user!=null&&user.getUstatus()== SysConstant.ACTIVE){
                request.getSession().setAttribute("user",user);
                //request.setAttribute("user",user);
                return SysConstant.FORWARD+SysConstant.FALG+"index.jsp";
                //request.getRequestDispatcher("/index.jsp").forward(request,response);
            }else{
                request.setAttribute("msg","用户名或者密码错误");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return SysConstant.FORWARD+SysConstant.FORWARD+"login.jsp";
    }

    @ContentType(value = "image/jpg")
    public void checkCode(HttpServletRequest request, HttpServletResponse response) {
        ValidateCode code = new ValidateCode(100,30,4,30);
        String vcode = code.getCode();
        request.getSession().setAttribute("vcode",vcode);
        try {
            code.write(response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @ContentType(value = "application/json;charset=utf-8")
    public void checkName(HttpServletRequest request, HttpServletResponse response){
        String username = request.getParameter("username");
        boolean b = userService.checkName(username);
        Map<String,Object> map = new HashMap<>();
        if(b){
            map.put("key",1);
        }else{
            map.put("key",0);
        }
        String json = new Gson().toJson(map);
        try {
            response.getWriter().write(json);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
